<?php 
//商品分类的控制器
class CategoryController extends Controller{

	public $layout = '//layouts/group';

	//增加分类
	public function actionAddcate(){
		$category=new Category();
		$data=$category->getallarray();	
		//得到所有的分类
		$data=$category->fenlei($data);
		//得到父级分类
		$arr=$category->getpidinfo();
		if(!empty($_POST['Category'])){
			$category->attributes=$_POST['Category'];
			if($category->save()){
				Yii::app()->user->setFlash('ok','添加成功');
				$this->redirect(array('category/index'));
			}			
		}
		$this->render('add',array('category'=>$category,'data'=>$data,'arr'=>$arr));
	}

	//分类列表展示
	public function actionIndex(){
		$category=Category::model();
		$data=$category->getallarray();	
		//实现无限极分类获取数据
		$datas=$category->fenlei($data);
		$this->render('index',array('datas'=>$datas));
	}
	//编辑
	public function actionEdit(){
		$category=Category::model();
		$data=$category->getallarray();	
		$data=$category->fenlei($data);
		$info=$category->findByPk($_GET['cid']);
		if(!empty($_POST['Category'])){
		$info->attributes=$_POST['Category'];
		if($info->save()){
				Yii::app()->user->setFlash('ok','修改成功');
				$this->redirect(array('category/index'));
			}			
		}
		$this->render('edit',array('category'=>$info,'data'=>$data));

	}
	//删除
	public function actionDel(){
		//先判断是否有子类
		$category=Category::model();
		$cid=$_GET['cid'];
		$num=$category->son($cid);
		if($num>=1){
			Yii::app()->user->setFlash('ok','删除失败,请先删除子分类');
			$this->redirect(array('category/index'));
			exit;
		}
		if($category->deleteByPk($cid)){
			Yii::app()->user->setFlash('ok','删除成功');
			$this->redirect(array('category/index'));
		}



	}




}